﻿<%@ WebHandler Language="C#" Class="ProductTrackingHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.SessionState;
using eBay.Service.Core.Soap;
using ebay.BLL;

public class ProductTrackingHandler : IHttpHandler,IRequiresSessionState
{    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text";
        string xgnum = "";
        string retCode = "";
        string retMsg = "";
        string userId = " ";
        int fEmptId = -1;
               
        string cmd = context.Request["cmd"];
        if (string.IsNullOrEmpty(cmd))
        {
            retCode = "1";
            retMsg = "操作错误";
        }
        if (context.Session["UserId"] == null)
        {
            retCode = "1001";
            retMsg = "您还没有登陆,或登录已超时,请重新登陆!";
            cmd = "";
        }
        else
        {
            userId = context.Session["UserId"].ToString();
            fEmptId = Convert.ToInt32(context.Session["FitemId"]);
        }
        switch (cmd)
        {
            case "alterprice"://修改价格
                {
                    string productId = context.Request["pid"];
                    productId = WebUtility.InputText(productId, 255);
                    EbayActiveListing activelisting = new EbayActiveListing();
                    DataTable table = activelisting.GetItemsByProductId(productId);
                    bool checkParas = true;
                    if (checkParas)
                    {
                        if (table==null||table.Rows.Count == 0)
                        {
                            retCode = "1";
                            retMsg = "商品编码解析有误！..";
                            checkParas = false;
                        }
                    }
                    if (checkParas)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            //string ebayUserId = row["ebayUserId"].ToString();
                            //string token = row["token"].ToString();
                            //string itemId = row["ItemId"].ToString();
                            string ebayUserId = "bobob_uk";
                            string token = @"AgAAAA**AQAAAA**aAAAAA**FmroTg**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wCmIGgD5OGpA6dj6x9nY+seQ**60wBAA**AAMAAA**uy6rdCwGP86vPIuEp1BrtkxCqzvfwbRlJTh7rTojmKrkXxZgmWOoNAwKVPVXnpBgySchaVHBK+pnaFl95v9PBMNIg/O4jN1VkNwiPQKO+3QW3DWCPWxmXwj5nE/YDwakV5i/+YpPXq2wNLx9PAqE+Nevmt13XWlJy3OHNgYwKb3aHViXox7CjdyT+BvjHYVwYLgBVnt8SybEVEG9rGu3kKFCtuYHgihZZjPVu8IMSUtPFZaMU/Xb4t09NVvpK6Cn/y/YR5KPb2bDJ027nsNGlSb+trbnXb2sJJyMqQD0MLEIG/FE1x0Jf6+g5ku3OTdX2UCr6/ZIXMe4+nVkR061GGh9ZfJ8kg/yKfK5TsZHgjEvYDRdO1BPZ9c+ihfnajQwroYaOBCasenrDr/XHBYVvfmba6l11SbBYBfrPx9ntsyVkzMnlWiSq5M+N5z7rM4ZgS4+PFz2nFGTic79bhnm0uenR0/5b0RZQWV1YPX7/60itSh2ap/WxTP4nK9uUqSIlOksQAYKeln9DT6lqCff4jwasMEjHN86bjod+uiUnvbG8svYFMhUlNo09DJjt4Nbc9+Ksp+Xig1mgRMfAhGBQK1xoha6j6iuvRXgBqhZ7DIASWUVlkkXM1wHTOMy1bw6nQJP7tJraYQhs5UYua8yVr9cdk5kJRlB9M0EHflzP8OwbqOtolnGw+1oFGVlzNRJ9kS6hFgzBz/qkfrey5qHqG8lMKhagNXIApq3iG8c8CIxTTPQ0ZzZTj9tx5/aw4iL";
                            string itemId = "220989448998";
                            //string itemId = "230769640558";
                            Item item = new Item();
                            ItemType OrignalItem = item.GetItem(itemId, token);
                            ItemType UpdateItem = new ItemType();
                            UpdateItem.ItemID = OrignalItem.ItemID;
                            CurrencyCodeType currencyCode = OrignalItem.Currency;
                            double orignal_price = OrignalItem.SellingStatus.CurrentPrice.Value;
                            double modify_price = 0d;
                            if (orignal_price >= 60)
                            {
                                modify_price = 999.99d;
                            }
                            else
                            {
                                modify_price = 99.99d;
                            }
                            UpdateItem.StartPrice = new AmountType();
                            UpdateItem.StartPrice.currencyID = currencyCode;
                            UpdateItem.StartPrice.Value = modify_price;
                            try
                            {
                                DateTime startDate = OrignalItem.ListingDetails.StartTime;
                                double listing_fee = 0d;
                                DateTime date = item.ReviseItem(UpdateItem, token, ref listing_fee);//修改价格
                                if (date == startDate && date != DateTime.Now)
                                {
                                    ebay.Dal.ItemChangePriceHis itemChange = new ebay.Dal.ItemChangePriceHis();
                                    bool val = itemChange.Insert(ebayUserId, itemId, productId, orignal_price, modify_price, listing_fee, userId, "修改价格");
                                    if (!val)
                                    {
                                        retCode = "1";
                                        retMsg = "数据出错，请重新修改！..";
                                    }
                                }
                                else
                                {
                                    retCode = "1";
                                    retMsg = "价格修改失败,请重新修改！.. 出错ItemId：" + itemId;
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.WriteLog(ex.ToString());
                                retCode = "1";
                                retMsg = "价格修改失败,原因：" + ex.Message;
                                continue;
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(retMsg))
                    {
                        retCode = "1";
                    }
                    else
                    {
                        retCode = "0";
                    }
                }
                break;
            case "salestop"://下架操作
                {
                    string productId = context.Request["pid"];
                    productId = WebUtility.InputText(productId, 255);
                    EbayActiveListing activelisting = new EbayActiveListing();
                    DataTable table = activelisting.GetItemsByProductId(productId);
                    bool checkParas = true;
                    if (checkParas)
                    {
                        if (table == null || table.Rows.Count == 0)
                        {
                            retCode = "1";
                            retMsg = "商品编码解析有误！..";
                            checkParas = false;
                        }
                    }
                    if (checkParas)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            //string ebayUserId = row["ebayUserId"].ToString();
                            //string token = row["token"].ToString();
                            //string itemId = row["ItemId"].ToString();
                            string ebayUserId = "bobob_uk";
                            string token = @"AgAAAA**AQAAAA**aAAAAA**FmroTg**nY+sHZ2PrBmdj6wVnY+sEZ2PrA2dj6wCmIGgD5OGpA6dj6x9nY+seQ**60wBAA**AAMAAA**uy6rdCwGP86vPIuEp1BrtkxCqzvfwbRlJTh7rTojmKrkXxZgmWOoNAwKVPVXnpBgySchaVHBK+pnaFl95v9PBMNIg/O4jN1VkNwiPQKO+3QW3DWCPWxmXwj5nE/YDwakV5i/+YpPXq2wNLx9PAqE+Nevmt13XWlJy3OHNgYwKb3aHViXox7CjdyT+BvjHYVwYLgBVnt8SybEVEG9rGu3kKFCtuYHgihZZjPVu8IMSUtPFZaMU/Xb4t09NVvpK6Cn/y/YR5KPb2bDJ027nsNGlSb+trbnXb2sJJyMqQD0MLEIG/FE1x0Jf6+g5ku3OTdX2UCr6/ZIXMe4+nVkR061GGh9ZfJ8kg/yKfK5TsZHgjEvYDRdO1BPZ9c+ihfnajQwroYaOBCasenrDr/XHBYVvfmba6l11SbBYBfrPx9ntsyVkzMnlWiSq5M+N5z7rM4ZgS4+PFz2nFGTic79bhnm0uenR0/5b0RZQWV1YPX7/60itSh2ap/WxTP4nK9uUqSIlOksQAYKeln9DT6lqCff4jwasMEjHN86bjod+uiUnvbG8svYFMhUlNo09DJjt4Nbc9+Ksp+Xig1mgRMfAhGBQK1xoha6j6iuvRXgBqhZ7DIASWUVlkkXM1wHTOMy1bw6nQJP7tJraYQhs5UYua8yVr9cdk5kJRlB9M0EHflzP8OwbqOtolnGw+1oFGVlzNRJ9kS6hFgzBz/qkfrey5qHqG8lMKhagNXIApq3iG8c8CIxTTPQ0ZzZTj9tx5/aw4iL";
                            string itemId = "220989448998";
                            Item item = new Item();
                            ItemType modifyitem = item.GetItem(itemId, token);
                            double listing_fee = 0;
                            double orignal_price = modifyitem.SellingStatus.CurrentPrice.Value;
                            double modify_price = orignal_price;
                            try
                            {
                                string mStatus = item.EndItem(modifyitem, token);//下架
                                if (!string.IsNullOrEmpty(mStatus))
                                {
                                    retMsg += "ProductId：" + productId + "ItemId:" + itemId + "下架失败>>";
                                }
                                else
                                {
                                    ebay.Dal.ItemChangePriceHis itemChange = new ebay.Dal.ItemChangePriceHis();
                                    bool val = itemChange.Insert(ebayUserId, itemId, productId, orignal_price, modify_price, listing_fee, userId, "商品下架");
                                    if (!val)
                                    {
                                        retCode = "1";
                                        retMsg = "下架操作失败，请重新操作！..";
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Log.WriteLog(ex.ToString());
                                retMsg += "ProductId：" + productId + "ItemId:" + itemId + "下架失败>>原因:" + ex.Message;
                                continue;
                            }
                        }
                    }                  
                    if (!string.IsNullOrEmpty(retMsg))
                    {
                        retCode = "1";
                    }
                    else
                    {
                        retCode = "0";
                    }
                }
                break;
            case "spth"://商品替换
                {
                    string opid = context.Request["opid"];//旧商品id
                    string npid = context.Request["npid"];//新商品id
                    string oq = context.Request["oq"];//旧数量
                    string nq = context.Request["nq"];//新数量
                   // string hid = context.Request["hid"];//仓库
                    decimal onum = 0;
                    decimal nnum = 0;
                    bool checkParas = true;
                    ebay.Dal.ReturnsOrder returns = new ebay.Dal.ReturnsOrder();
                    DataTable parasdt = new DataTable();
                    if (!decimal.TryParse(oq, out onum))
                    {
                        retCode = "1";
                        retMsg = "数量不正确";
                        checkParas = false;
                    }
                    if (!decimal.TryParse(nq, out nnum))
                    {
                        retCode = "1";
                        retMsg = "数量不正确";
                        checkParas = false;
                    }
                    if (checkParas)
                    {
                        int val = returns.IsExists(npid);
                        if (val== 0)
                        {
                            retCode = "1";
                            retMsg = "类似产品不存在！..";
                            checkParas =false ;
                        }
                    }
                    if (checkParas)
                    {
                        parasdt = returns.GetReplaceParas(opid);//包含ebayuserId, orderLineitemid
                        if (parasdt == null || parasdt.Rows.Count == 0)
                        {
                            retCode = "1";
                            retMsg = "旧产品现无订单需要替代项！..";
                            checkParas = false;
                        } 
                    }
                    if (checkParas)
                    {
                        bool falg=false ;
                        falg = returns.ProductReplace(parasdt, nnum, onum, opid, npid, userId, ref retMsg);//替换
                        if (falg)
                        {
                            retCode = "0";
                        }
                        else 
                        {
                            retCode = "1";
                        }
                    }  
                }
                break;
            case "shortage"://盘亏单写K3
                {
                    string num = context.Request["qty"];
                    string productId = context.Request["pid"];
                    productId = WebUtility.InputText(productId, 255);
                    string stock = context.Request["stockid"];
                    stock = WebUtility.InputText(stock, 255);
                    EbayActiveListing activelisting = new EbayActiveListing();
                    DataTable table = activelisting.GetItemsByProductId(productId);
                    bool checkParas = true;
                    decimal fqty = 0;
                    int stockid = 0;
                    int fitemId = 0;
                    if (checkParas)
                    {
                        if (!decimal.TryParse(num, out fqty))
                        {
                            retCode = "1";
                            retMsg = "数量不正确";
                            checkParas = false;
                        }
                    }
                    if (checkParas)
                    {
                        if (string.IsNullOrEmpty(productId))
                        {
                            retCode = "1";
                            retMsg = "商品编号有误！..";
                            checkParas = false;
                        }
                    }
                    if (checkParas)
                    {
                        if (!int.TryParse(stock, out stockid))
                        {
                            retCode = "1";
                            retMsg = "仓库编号不正确";
                            checkParas = false;
                        }
                    }
                    if (checkParas)
                    {
                       if (table == null || table.Rows.Count == 0)                       
                        {
                            retCode = "1";
                            retMsg = "商品编码解析有误！..";
                            checkParas = false;
                        }                    
                    }
                    if (checkParas)
                    {
                        try
                        {
                            foreach (DataRow r in table.Rows)
                            {
                                string fItemid = r["FitemId"].ToString();
                                if (!int.TryParse(fItemid, out fitemId))
                                {
                                    retCode = "1";
                                    retMsg = "商品编码不正确";
                                    checkParas = false;
                                }
                                
                                ebay.Dal.ReturnsOrder returns = new ebay.Dal.ReturnsOrder();
                                bool val = returns.Shortage(productId, fEmptId, stockid, fitemId, fqty, userId, ref retMsg);//盘亏单写K3                        
                                if (val)
                                {
                                    retCode = "0";
                                }
                                else
                                {
                                    retCode = "1";
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            retCode = "1";
                            Log.WriteLog(ex.ToString());
                            retMsg += "ProductId：" + productId + "盘亏单写K3失败>>原因:" + ex.Message;
                        }
                    }
                }
                break;   
                                             
            default :
                break;
        }
        context.Response.Write("[{\"retCode\":\"" + retCode + "\",\"retMsg\":\"" + retMsg + "\",\"xgnum\":\"" + xgnum + "\"}]");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }   
}